package org.example.MusicPlayAutoTest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import javax.print.attribute.standard.Destination;
import java.util.Date;

import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.SECONDS;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Tests extends InitBrowser {
//通过junit的多参数化功能进行登录设计
    @Order(1)
    @ParameterizedTest
    @CsvSource({"'http://127.0.0.1:8080/login.html','bit','http://127.0.0.1:8080/list.html','123456'"})
    void Login(String curUrl, String username, String excepted_jump_url, String password) throws InterruptedException {

        webDriver.get(curUrl);
        webDriver.findElement(By.cssSelector("#user")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        // sleep(1000);
        webDriver.manage().timeouts().implicitlyWait(3, SECONDS);
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, SECONDS);
        sleep(1000);
        webDriver.switchTo().alert().accept();
        //  sleep(1000);
        webDriver.manage().timeouts().implicitlyWait(3, SECONDS);

        if (excepted_jump_url.equals(webDriver.getCurrentUrl())) {
            System.out.println("登录测试通过");
        } else {
            System.out.println("登录测试不通过");
        }


    }


    //
//    @ParameterizedTest
//    @CsvSource({"'http://127.0.0.1:8080/login.html','xiaomi','http://127.0.0.1:8080/list.html','123456'",
//            "'http://127.0.0.1:8080/login.html','xiaohua','http://127.0.0.1:8080/list.html','000'"})
//    void LoginError(String curUrl, String username ,String excepted_jump_url,String password) throws InterruptedException {
//
//        webDriver.get(curUrl);
//        webDriver.findElement(By.cssSelector("#user")).sendKeys(username);
//        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
//        // sleep(1000);
//        webDriver.manage().timeouts().implicitlyWait(3,SECONDS);
//        webDriver.findElement(By.cssSelector("#submit")).click();
//        webDriver.manage().timeouts().implicitlyWait(3,SECONDS);
//        sleep(1000);
//        webDriver.switchTo().alert().accept();
//        //  sleep(1000);
//        webDriver.manage().timeouts().implicitlyWait(3,SECONDS);
//
//        if(!excepted_jump_url.equals(webDriver.getCurrentUrl())){
//            System.out.println("测试通过");
//        }else{
//            System.out.println("测试不通过");
//        }
//
//
//    }
//上传音乐功能
    @Order(2)
    @ParameterizedTest
    @CsvSource({"'http://127.0.0.1:8080/list.html','D:\\work\\葫芦娃.mp3','未知'",
            "'http://127.0.0.1:8080/list.html','D:\\work\\一路向北.mp3','周杰伦'",
            "'http://127.0.0.1:8080/list.html','D:\\work\\光年之外.mp3','周杰伦'"})
    void update(String curUrl, String musicPath, String singer) throws InterruptedException {
        sleep(1000);

        webDriver.findElement(By.xpath("//*[@id=\"body\"]/div[1]/div[2]/a[2]")).click();
        sleep(1000);
        webDriver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).sendKeys(musicPath);
        sleep(1000);
        webDriver.findElement(By.cssSelector("body > form > label > input[type=text]")).sendKeys(singer);
        sleep(1000);
        webDriver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
        sleep(1000);

        Assertions.assertEquals(curUrl, webDriver.getCurrentUrl());
        System.out.println("上传测试通过");

    }
//收藏音乐
    @Order(3)
    @Test
    void likeMusic() throws InterruptedException {
        webDriver.findElement(By.cssSelector("#info > tr > td:nth-child(5) > button:nth-child(2)")).click();
        sleep(1000);
        boolean ret = webDriver.switchTo().alert().getText().equals("收藏成功！");
        System.out.println("弹窗文本：" + webDriver.switchTo().alert().getText());
        if (ret) {
            System.out.println("收藏音乐测试通过");
        } else {
            System.out.println("收藏音乐测试不通过");
        }
        sleep(1000);
        webDriver.switchTo().alert().accept();
    }
//重复收藏
    @Order(4)
        @Test //重复收藏同一首歌收藏失败
    void failLikeMusic() throws InterruptedException {
        webDriver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(5) > button:nth-child(2)")).click();
        sleep(1000);
        boolean ret = webDriver.switchTo().alert().getText().equals("收藏成功!");
        if (ret) {
            System.out.println("重复收藏音乐不测试通过");
        } else {
            System.out.println("重复收藏测试通过");
        }
        sleep(1000);
        webDriver.switchTo().alert().accept();

    }
    //删除音乐
@Test
@Order(5)
    void deleteMusic() throws InterruptedException {
        webDriver.findElement(By.cssSelector("#info > tr > td:nth-child(5) > button:nth-child(1)")).click();
        sleep(1500);
        boolean ret = webDriver.switchTo().alert().getText().equals("删除成功！,重新加载当前页面！");

        if(ret){
            webDriver.switchTo().alert().accept();
            if(webDriver.getCurrentUrl().equals("http://127.0.0.1:8080/list.html")){
                System.out.println("删除音乐测试通过");
            }
        }else {
            System.out.println("删除音乐测试不通过");
        }

    }
    //搜索音乐
    @Test
    @Order(6)
    void searchMusic() throws InterruptedException {
       // webDriver.findElement(By.cssSelector("#exampleInputName2")).click();
        webDriver.findElement(By.cssSelector("#exampleInputName2")).sendKeys("光年");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#submit1")).click();
        sleep(1000);
        if(webDriver.getCurrentUrl().equals("http://127.0.0.1:8080/list.html")){
            System.out.println("查询测试通过");
        }else {
            System.out.println("查询测试不通过");
        }

    }

}